package twoRegInstructions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import main.MIPSSimulator;

public class Srl extends MIPSTwoRegInstruction {
	public Srl(String input) throws FileNotFoundException, IOException {
		myRecognizedRegisters.load(new FileInputStream(new File(
		"recognizedRegisters.properties")));
		myRegisterOne = myRecognizedRegisters.getProperty(input.substring(16,
				21));
		myRegisterTwo = myRecognizedRegisters.getProperty(input
				.substring(11, 16));
		String immediateValueString = input.substring(21, 26);

		int temp = Integer.parseInt(immediateValueString, 2);
		byte[] b = new byte[4];
		for (int k = 0; k < 4; k++) {
			int offset = (b.length - 1 - k) * 8;
			b[k] = (byte) ((temp >>> offset) & 0xFF);
		}
		StringBuffer sb = new StringBuffer(b.length * 2);
		char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		for (int k = 0; k < b.length; k++) {
			sb.append(hexChar[(b[k] & 0xf0) >>> 4]);
			sb.append(hexChar[b[k] & 0x0f]);
		}
		String hexstring = sb.toString();
		myImmediateValueString = "0x"
				+ hexstring.substring(hexstring.length() - 4, hexstring
						.length());
		myName = "srl";
	}

	@Override
	public void execute() {
		int regTwo = MIPSSimulator.myRegisters.get(myRegisterTwo);
		MIPSSimulator.myRegisters.put(myRegisterOne, regTwo >>> myImmediateValue);
	}
}
